<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">旅游后台管理系统</div>
      <el-form class="ms-content" :model="param" :rules="rules" ref="loginForm" label-width="0px">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="username">
            <template #prepend>
              <el-button :icon="User"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="password" show-password v-model="param.password" @keyup.enter="submitForm">
            <template #prepend>
              <el-button :icon="Lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm" data-test="loginBtn">登录</el-button>
        </div>
        <p class="login-tips">Tips : 请输入管理员账号和密码。</p>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, unref, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import request from "../../utils/request";
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/userStore'; // 假设路径  

const userStore = useUserStore(); // 引入用户 store 
console.log(userStore);


const param = reactive({
  username: "",
  password: "",
});
const rules = {
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: ["blur", "change"],
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: ["blur", "change"] },
    { min: 3, max: 10, message: "密码位数只能在3~10之间", trigger: ["blur", "change"] },
  ],
};
const loginForm = ref(null);
const router = useRouter();
const submitForm = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      request
        .request({
          url: "/user/login",
          method: "post",
          data: {
            username: param.username,
            password: param.password,
          },
        })
        .then((res) => {
          if (res.data.code == "200" && res.data.msg == "success") {
            ElMessage({
              message: `恭喜,${param.username}登录成功`,
              type: "success",
            });
            userStore.setUser(res.data.data);//pinia
            //vueX
            localStorage.setItem(
              "userInfo",
              JSON.stringify({
                user: res.data.data,
                // accessToken: res.data.data.accessToken,
              })
            );
            router.push("/");
          } else if (res.data.code == "201" || res.data.code == "500") {
            ElMessage({
              message: res.data.msg,
              type: "error",
            });
          } else if (res.data.code == "202" && res.data.msg == "fail") {
            ElMessage({
              message: `${param.username}管理员已停用，暂无权限`,
              type: "error",
            });
          } else if (res.data.code == "203" && res.data.msg == "fail") {
            ElMessage({
              message: `${param.username}不是管理员，暂无权限`,
              type: "error",
            });
          } else if (res.data.code == "404" && res.data.msg == "fail") {
            ElMessage({
              message: `${param.username}用户不存在,请重试`,
              type: 'error',
            });
          }
        })
        .catch((e) => {
          console.log(e);
          ElMessage({
            message: `登录失败,请重试`,
            type: 'error',
          })
        });
    } else {
      ElMessage({
        message: `登录失败,请重试`,
        type: "error",
      });
    }
  });
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-size: 100%;
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
